# -*- coding: utf-8 -*-

import tensorflow as tf
import numpy as np

# 线性模型 demo
# 随机生成100个点
x_data = np.random.rand(100)
y_data = 0.2 * x_data + 0.3

# 构造线性模型
w = tf.Variable(0.)
b = tf.Variable(0.)
y = w * x_data + b

# 平方损失函数
# ((y - y_data) ^ 2) 之和 / N
loss = tf.reduce_mean(tf.square(y_data - y))

# 梯度下降优化器, 寻找最佳的w和b
optimizer = tf.train.GradientDescentOptimizer(0.3)
# 最小化损失函数
train = optimizer.minimize(loss)

init = tf.global_variables_initializer()

with tf.Session() as sess:
    sess.run(init)
    for step in range(201):
        sess.run(train)
        if step % 20 == 0:
            print(step, sess.run([w, b]))
